package com.ljw.vendingmachine;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.media.AudioManager;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.SeekBar;
import android.widget.Toast;

import com.ljw.vendingmachine.base.BaseActivity;
import com.ljw.vendingmachine.bean.MessageEvent;
import com.ljw.vendingmachine.utils.Constant;
import com.ljw.vendingmachine.utils.SPUtils;

import org.greenrobot.eventbus.EventBus;

import butterknife.BindView;

public class SettingActivity extends BaseActivity {

    @BindView(R.id.layout_password)
    LinearLayout layout_password;
    @BindView(R.id.ed_password)
    EditText ed_password;
    @BindView(R.id.btn_yes)
    Button btn_yes;
    @BindView(R.id.layout_settings)
    LinearLayout layout_settings;
    @BindView(R.id.ed_IP)
    EditText ed_IP;
    @BindView(R.id.btn_IPSava)
    Button btn_IPSava;
    @BindView(R.id.ed_port)
    EditText ed_port;
    @BindView(R.id.btn_portSava)
    Button btn_portSava;
    @BindView(R.id.ed_deviceID)
    EditText ed_deviceID;
    @BindView(R.id.btn_deviceSava)
    Button btn_deviceSava;
    @BindView(R.id.ed_number)
    EditText ed_number;
    @BindView(R.id.btn_numberSava)
    Button btn_numberSava;
    @BindView(R.id.progress_volume)
    SeekBar seekBar_volume;
    @BindView(R.id.btn_exit)
    Button btn_exit;
    @Override
    public int setContentView() {
        return R.layout.activity_setting;
    }


    @Override
    public void initView() {
        String what = getIntent().getStringExtra("what");

        if (SPUtils.contains(this, Constant.PASSWORD)){
            layout_password.setVisibility(View.VISIBLE);
            layout_settings.setVisibility(View.GONE);
        }else {
            layout_password.setVisibility(View.GONE);
            layout_settings.setVisibility(View.VISIBLE);
        }

        btn_yes.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String password = ed_password.getText().toString().trim();
                if (!TextUtils.isEmpty(password)&&SPUtils.get(SettingActivity.this,Constant.PASSWORD,"").toString().equals(password)){
                    layout_password.setVisibility(View.GONE);
                    layout_settings.setVisibility(View.VISIBLE);
                }
            }
        });

        if (SPUtils.contains(this,Constant.IP)){
            String ip= (String) SPUtils.get(this,Constant.IP,"");
            ed_IP.setText(ip);
        }
        if (SPUtils.contains(this,Constant.DEVICEID)){
           String deviceid= (String) SPUtils.get(this,Constant.DEVICEID,"");
            ed_deviceID.setText(deviceid);
        }
        btn_deviceSava.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String deviceid = ed_deviceID.getText().toString().trim();
                if (TextUtils.isEmpty(deviceid)){
                    Toast.makeText(SettingActivity.this,"ID不能为空",Toast.LENGTH_LONG).show();
                    return;
                }
                SPUtils.put(SettingActivity.this,Constant.DEVICEID,deviceid);
                Toast.makeText(SettingActivity.this,"ID设置成功",Toast.LENGTH_LONG).show();
            }
        });

        if (SPUtils.contains(this,Constant.NUMBERKEY)){
            int number= (int) SPUtils.get(this,Constant.NUMBERKEY,1);
            ed_number.setText(String.valueOf(number));
        }
        btn_numberSava.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String trim = ed_number.getText().toString().trim();
                if (TextUtils.isEmpty(trim)){
                    Toast.makeText(SettingActivity.this,"不能为空",Toast.LENGTH_LONG).show();
                    return;
                }
                try{
                    int anInt = Integer.parseInt(trim);
                    Constant.NUMBER=anInt;
                    SPUtils.put(SettingActivity.this,Constant.NUMBERKEY, anInt);
                    Toast.makeText(SettingActivity.this,"设置成功",Toast.LENGTH_LONG).show();
                }catch (Exception e){
                    Toast.makeText(SettingActivity.this,"请输入正整数",Toast.LENGTH_LONG).show();
                }
            }
        });
        AudioManager audioManager = (AudioManager) this.getSystemService(Context.AUDIO_SERVICE);
        //最大音量
        int maxVolume = audioManager.getStreamMaxVolume(AudioManager.STREAM_MUSIC);
        int currentVolume = audioManager.getStreamVolume(AudioManager.STREAM_MUSIC);
        seekBar_volume.setMax(maxVolume);
        seekBar_volume.setProgress(currentVolume);
        seekBar_volume.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
            @Override
            public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
                audioManager.setStreamVolume(AudioManager.STREAM_MUSIC, progress, AudioManager.FLAG_SHOW_UI);
            }

            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {

            }

            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {

            }
        });
        btn_exit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (!SPUtils.contains(SettingActivity.this,Constant.DEVICEID)){
                    Toast.makeText(SettingActivity.this,"ID不能为空",Toast.LENGTH_LONG).show();
                    return;
                }
                startActivity(new Intent(SettingActivity.this,MainActivity.class));
                MessageEvent mes = new MessageEvent();
                mes.setCode(Constant.CONNECT);
                //       mes.setData(String.format(Constant.COMD_GOODS_OUT_SECCESS, getQRcode(MainActivity.goodsMap), orderNo));
                EventBus.getDefault().post(mes);
                finish();

            }
        });
    }

    @Override
    public void initData() {

    }
}
